package com.ruoyi.project.manhua.webLook.service.impl;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.manhua.collect.domain.MhCollect;
import com.ruoyi.project.manhua.name.domain.MhName;
import com.ruoyi.project.manhua.newXml.MhCollectNewMapper;
import com.ruoyi.project.manhua.newXml.MhNameNewMapper;
import com.ruoyi.project.manhua.webLook.entity.MhWebCollect;
import com.ruoyi.project.manhua.webLook.enumClass.exceptionEnum;
import com.ruoyi.project.manhua.webLook.service.MhPersonageService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class MhPersonageServiceImpl implements MhPersonageService {


    /*漫画 类*/
    @Autowired
    private MhNameNewMapper mhNameNewMapper;

    /*七牛云 工具类*/
    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    /*用户收藏类*/
    @Autowired
    private MhCollectNewMapper mhCollectNewMapper;


    /**
     * 个人页面获取 用户 收藏信息
     *
     * @return
     */
    @Override
    public AjaxResult personageDataService() {

        AjaxResult ajaxResult = new AjaxResult();

        /*获取用户信息*/
        User sysUser = ShiroUtils.getSysUser();

        /*如果用户没有登入 抛出异常*/
        // exceptionEnum.Checker.check(ObjectUtils.isEmpty(sysUser),ex.user_null);
        if (ObjectUtils.isEmpty(sysUser)) {

            ajaxResult.put("code", exceptionEnum.user_null.getCode());
            ajaxResult.put("msg", exceptionEnum.user_null.getMsg());

            return ajaxResult;
        }

        /*查看 用户收藏的漫画*/
        List<MhCollect> mhCollects = mhCollectNewMapper.selectWebMhCollectList(new MhWebCollect(sysUser.getUserId()));

        /*如果没有收藏信息 抛出异常*/
        // exceptionEnum.Checker.check(CollectionUtils.isEmpty(mhCollects),ex.personage_collect_null);
        if (CollectionUtils.isEmpty(mhCollects)) {
            ajaxResult.put("code", exceptionEnum.personage_collect_null.getCode());
            ajaxResult.put("msg", exceptionEnum.personage_collect_null.getMsg());

            sysUser.setDept(null);
            ajaxResult.put("user",sysUser);
            return ajaxResult;
        }

        /*有收藏信息 查询出漫画 使用stream流将mhCollects漫画id取出 转换为集合 一次性取出所有收藏数据 */
        List<MhName> mhNames = mhNameNewMapper.selectMhNameByList(mhCollects.stream().map((e) -> e.getNameId()).collect(Collectors.toList()));

        /*数据 转化 编码*/
        mhNames.forEach((e) -> {
            e.setTheCover(
                    Constants.getEncoder(
                            qiNiuYunUtils.getUrlImg(
                                    e.getTheCover())));
        });


        if (!CollectionUtils.isEmpty(mhCollects) && !CollectionUtils.isEmpty(mhNames)) {
            /*收藏数据*/
            ajaxResult.put("collectData", mhNames);
            sysUser.setDept(null);
            ajaxResult.put("user",sysUser);
            return ajaxResult;
        }

        return new AjaxResult().error();
    }


}
